'use strict'
/**
 * MessageUtil
 * 消息显示工具类
 * 你是一段成熟的代码，要学会自己改bug  ----我是小尾巴
 */

import {isMobile} from './BrowserUtil'
import {oneOf} from './DataUtil'
import {messageType} from './config/message.config.js'

import {
  Message as $Message,
  Notice as $Notice,
  Modal as $Modal
} from 'iview'

const messageTypeArray = Object.values(messageType)

const method = {
  modal: (content, param = {}, type) => {
    if (!oneOf(type, messageTypeArray)) {
      content = '内容弹框参数错误'
      type = messageType.ERROR
    }
    if (isMobile()) {
      alert(content)
    } else {
      param['content'] = content
      if (!param.title) {
        param.title = '消息通知'
      }
      $Modal[type](param)
    }
  },
  notice: (content, param = {}, type) => {
    if (!oneOf(type, messageTypeArray)) {
      content = '内容弹框参数错误'
      type = messageType.ERROR
    }
    param['desc'] = content
    if (!param.title) {
      param.title = '消息通知'
    }
    $Notice[type](param)
  }
}

export default {
  message: {
    /**
     * 消息提示
     * @param content
     * @param param
     */
    info (content, param = {}) {
      $Message.info(content)
    },
    /**
     * 错误消息提示
     * @param content
     * @param param
     */
    error (content, param = {}) {
      $Message.error(content)
    },
    /**
     * 警告消息提示
     * @param content
     * @param param
     */
    warning (content, param = {}) {
      $Message.warning(content)
    },
    /**
     * 成功消息提示
     * @param content
     * @param param
     */
    success (content, param = {}) {
      $Message.success(content)
    }
  },
  notice: {
    /**
     * 消息通知
     * @param content
     * @param param
     */
    info (content, param = {}) {
      method.notice(content, param, messageType.INFO)
    },
    /**
     * 错误消息通知
     * @param content
     * @param param
     */
    error (content, param = {}) {
      method.notice(content, param, messageType.ERROR)
    },
    /**
     * 错误消息通知
     * @param content
     * @param param
     */
    warning (content, param = {}) {
      method.notice(content, param, messageType.WARNING)
    },
    /**
     * 成功消息通知
     * @param content
     * @param param
     */
    success (content, param = {}) {
      method.notice(content, param, messageType.SUCCESS)
    }
  },
  modal: {
    /**
     * 消息通知
     * @param content
     * @param param
     */
    info (content, param = {}) {
      method.modal(content, param, messageType.INFO)
    },
    /**
     * 错误消息通知
     * @param content
     * @param param
     */
    error (content, param = {}) {
      method.modal(content, param, messageType.ERROR)
    },
    /**
     * 错误消息通知
     * @param content
     * @param param
     */
    warning (content, param = {}) {
      method.modal(content, param, messageType.WARNING)
    },
    /**
     * 成功消息通知
     * @param content
     * @param param
     */
    success (content, param = {}) {
      method.modal(content, param, messageType.SUCCESS)
    }
  },
  /**
   * 确认框
   * @param content
   * @param ok
   * @param cancel
   * @param params
   */
  confirm (content = '', ok = () => {}, cancel = () => {}, params = {}) {
    $Modal.confirm({
      title: params.title || '提示',
      content: `<p>${content}</p>`,
      onOk: ok,
      onCancel: cancel
    })
  }
}
